package com.example.stock.http;

import android.net.ParseException;

import com.example.stock.config.Config;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializer;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.io.NotSerializableException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;

import retrofit2.HttpException;

public class ApiException extends Exception {

    private final int code;
    private String message;

    public ApiException(Throwable throwable, int code) {
        super(throwable);
        this.code = code;
        this.message = throwable.getMessage();
    }

    public int getCode() {
        return code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public static ApiException handleException(Throwable e) {
        ApiException ex;
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ApiException(httpException, httpException.code());
            ex.message = "网络错误" + httpException.code();
            return ex;
        } else  if (e instanceof JsonParseException
                || e instanceof ParseException
                || e instanceof JSONException
                || e instanceof JsonSerializer
                || e instanceof NotSerializableException
                ) {
            ex = new ApiException(e, Config.PARSE_ERROR);
            ex.message = "解析错误";
            return ex;
        } else

            if (e instanceof ClassCastException) {
            ex = new ApiException(e, Config.CAST_ERROR);
            ex.message = "类型转换错误";
            return ex;
        } else if (e instanceof ConnectException) {
            ex = new ApiException(e, Config.NETWORD_ERROR);
            ex.message = "连接失败";
            return ex;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            ex = new ApiException(e, Config.SSL_ERROR);
            ex.message = "证书验证失败";
            return ex;
        } else if (e instanceof ConnectTimeoutException) {
            ex = new ApiException(e, Config.TIMEOUT_ERROR);
            ex.message = "连接超时";
            return ex;
        } else if (e instanceof java.net.SocketTimeoutException) {
            ex = new ApiException(e, Config.TIMEOUT_ERROR);
            ex.message = "连接超时或无法连接到服务器";
            return ex;
        } else if (e instanceof UnknownHostException) {
            ex = new ApiException(e, Config.UNKNOWNHOST_ERROR);
            ex.message = "无法解析该域名";
            return ex;
        } else if (e instanceof NullPointerException) {
            ex = new ApiException(e, Config.NULLPOINTER_EXCEPTION);
            ex.message = e.getMessage();
            return ex;
        } else if (e instanceof UnknownServiceException) {
            ex = new ApiException(e, Config.NULLPOINTER_EXCEPTION);
            ex.message = "网络安全策略不允许,请设置正确的IP地址";
            return ex;
        }  else {
            ex = new ApiException(e, Config.UNKNOWN);
            ex.message = e.getMessage();
            return ex;
        }
    }

}
